package com.im.service;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.im.service.jim.command.DemoWsHandshakeProcessor;
import com.im.service.jim.listener.ImGroupListener;
import com.im.service.jim.listener.ImUserListener;
import com.im.service.jim.processor.BoundSessionProcessor;
import com.im.service.jim.processor.CustomMessageProcessor;
import com.im.service.jim.processor.LoginServiceProcessor;
import lombok.extern.slf4j.Slf4j;
import org.jim.core.config.ImConfig;
import org.jim.core.packets.Command;
import org.jim.server.JimServer;
import org.jim.server.command.CommandManager;
import org.jim.server.command.handler.ChatReqHandler;
import org.jim.server.command.handler.HandshakeReqHandler;
import org.jim.server.command.handler.LoginReqHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.config.PropertyImServerConfigBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @author liuzhiqiang
 */
@SpringBootApplication(
        scanBasePackages = {"com.im.service"},
        scanBasePackageClasses = {org.jim.core.cache.redis.RedisConfiguration.class}
)
@EnableScheduling
@Slf4j
public class ImApplication implements CommandLineRunner {

    @Autowired
    private LoginServiceProcessor loginServiceProcessor;
    @Autowired
    private ImUserListener imUserListener;
    @Autowired
    private CustomMessageProcessor customMessageProcessor;
    @Autowired
    private StringRedisTemplate redisTemplate;


    public static void main(String[] args) {
        SpringApplication.run(ImApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("ImApplication服务器已启动完毕");
        ImServerConfig imServerConfig = new PropertyImServerConfigBuilder("jim.properties").build();
        JimServer jimServer = new JimServer(imServerConfig);
        //设置群组监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImGroupListener(new ImGroupListener());
        //设置绑定用户监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImUserListener(imUserListener);

        HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
        //添加自定义握手处理器;
        handshakeReqHandler.addMultiProtocolProcessor(new DemoWsHandshakeProcessor());
        LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
        //添加登录业务处理器;
        loginReqHandler.setSingleProcessor(loginServiceProcessor);
        //添加用户业务聊天记录处理器，用户自己继承抽象类BaseAsyncChatMessageProcessor即可，以下为内置默认的处理器！
        ChatReqHandler chatReqHandler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ, ChatReqHandler.class);
        chatReqHandler.setSingleProcessor(customMessageProcessor);
        // 保持会话时清空未读数量
        BoundSessionProcessor boundSessionProcessor = CommandManager.getCommand(Command.BOUND_SESSION, BoundSessionProcessor.class);
        boundSessionProcessor.setRedisTemplate(redisTemplate);

        /*****************end *******************************************************************************************/
        jimServer.start();
        customMessageProcessor.setImServerConfig(ImConfig.Global.get());

        // 修改序列化 Long 转 String
        SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
        serializeConfig.put(Long.class, new ToStringSerializer());
    }
}

